<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button>post</button>
</body>
<script>
    var btn = document.querySelector("button");

    btn.onclick = function () {

        // post传参,post是通过HTTP post机制，将参数数据队列放在HTML HEADER内一起传送到接口所指的URL地址 => 看不见

        // ajax中如何以post方式传递数据?
        // 1. 将参数数据队列 作为参数 传到send中
        // 2. 通过请求头 将数据格式改为FormData

        // Content-Type: text/plain;charset=UTF-8   => text  , json

        var user = "aaaaaa123123";
        var pwd = 123123;
        var phone = "17386141412";
        var email = "12312355@qq.com"


        var xhr = new XMLHttpRequest(); // 0

        xhr.open("post", `http://43.138.81.225/demo/php/register.php`, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  // 将数据格式改为FormData

        xhr.send(`user=${user}&pwd=${pwd}&phone=${phone}&email=${email}`);

        xhr.onreadystatechange = function () {
            // 2  3  4
            console.log("readyState:", xhr.readyState);
            if (xhr.readyState == 4 && xhr.status == 200) {
                var result = xhr.responseText;
                result = JSON.parse(result);
                console.log(result);
            }
        }
    }




</script>

</html>